package com.adp.interceptor;

import com.adf.system.dataimport.DataImportItem;
import com.adf.system.dataimport.filetype.AbsFileTypeProcessor;
import com.adf.system.dataimport.interceptor.IDataImportInterceptor;
import com.adp.db.DBOperator;

import java.sql.Connection;
import java.util.List;

public class usingRegisterDataImport implements IDataImportInterceptor
{

    public boolean doImportStart(DataImportItem dataImportItem)
    {
        System.out.println("导入文件"+dataImportItem.getDatafileObj().getAbsolutePath());
        AbsFileTypeProcessor fileProcessor=dataImportItem.getFileProcessor();
        List<String> lstColTitles=fileProcessor.getLstColnameData();
        if(lstColTitles!=null&&lstColTitles.size()>0)
        {
            System.out.print("标题行：");
            for(String colTitle:lstColTitles)
            {
                System.out.print(colTitle+";;");
            }
            System.out.println();
        }
        if(fileProcessor.isEmpty())
        {
            System.out.println("此数据文件为空");
        }else
        {
            System.out.println("此数据文件所有记录数据依次如下所示：");
            int startdatarowidx=fileProcessor.getStartrecordindex();
            int recordcount=fileProcessor.getRecordcount();
            for(int i=startdatarowidx;i<startdatarowidx+recordcount;i++)
            {
                List lstRowData=fileProcessor.getRowData(i);
                if(lstRowData==null||lstRowData.size()==0) continue;
                for(int j=0;j<lstRowData.size();j++)
                {//依次打印此行的每一列数据
                    System.out.print(lstRowData.get(j)+";;");
                }
                System.out.println();
            }
        }
        return true;//继续导入操作
    }

    public boolean beforeImportRow(Connection conn, DataImportItem dataImportItem, List<String> lstTitleRowCols, List lstDataRowCols)
    {
        System.out.println("导入数据："+lstDataRowCols);
        //if (lstDataRowCols.get(3).toString().trim().equals("")) lstDataRowCols.set(3, "null");
        return true;
    }

    public void afterImportRow(Connection conn, DataImportItem dataImportItem, List<String> lstTitleRowCols, List lstDataRowCols)
    {
    	/*
        String sql = "";
        String sbillno = "";
        String sorderline = "";
        
        sbillno = lstDataRowCols.get(13).toString();
        sorderline = lstDataRowCols.get(14).toString();
        
        sql = String.format("update `tbl_u_using_bill` set id= CONCAT(billno,'-', orderline) where billno='%s' and orderline=%s;", sbillno, sorderline);
        DBOperator.dataExec(sql);
        */
    	
        System.out.println("导完数据："+lstDataRowCols);
    }

    public void doImportEnd(int success, DataImportItem dataImportItem, Exception e)
    {
        System.out.println("完成数据文件"+dataImportItem.getDatafileObj().getAbsolutePath()+"导入.......");
        if(success>0)
        {
            System.out.println("导入成功........");
            String sql = "";
            sql = String.format("update `tbl_u_using_bill` set id= CONCAT(billno,'-', orderline) where id=null;");
            DBOperator.dataExec(sql);
        }else
        {
            System.out.println("导入失败........");
        }
        if(e!=null)
        {//导数据过程中抛出了异常
            e.printStackTrace();
        }
    }
}
